#ifndef FACTORIAL_H
#define FACTORIAL_H
#include <sstream>
#include <string>
// factorial函数，计算阶乘，返回factorial!的计算结果，如5!=120
std::string factorial(int factorial)
{
    int LargeNumberFactorial[10000] = {0};
    LargeNumberFactorial[0] = 1; // 0! = 1
    LargeNumberFactorial[1] = 1; // 第一位对应的数值初始为 1
    int digit = 1;               // n!阶乘求取的位数初始为 1

    for (int i = 1; i <= factorial; i++) // 1,2,3,...,n
    {
        for (int j = 1; j <= digit; j++) {
            LargeNumberFactorial[j] *= i;
        }

        for (int j = 1; j <= digit; j++) {
            if (LargeNumberFactorial[j] > 10) {
                for (int r = 1; r <= digit; r++) {
                    if (LargeNumberFactorial[digit] > 9) {
                        digit++;
                    }
                    LargeNumberFactorial[r + 1] += LargeNumberFactorial[r] / 10;
                    LargeNumberFactorial[r] %= 10;
                }
            }
        }
    }
    std::stringstream ss;
    for (int k = digit; k > 0; k--) {
        ss << LargeNumberFactorial[k];
    }
    std::string result;
    ss >> result;
    return result;
}
#endif